package com.eve.eplus.sns.weibo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;
import android.os.Message;

import com.eve.eplus.common.app.BaseApplication;
import com.eve.eplus.common.app.Constants;
import com.eve.eplus.common.tools.LogTools;
import com.eve.eplus.sns.SNSContentManager;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.AccountAPI;
import com.weibo.sdk.android.api.FriendshipsAPI;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.UsersAPI;
import com.weibo.sdk.android.api.WeiboAPI.FEATURE;
import com.weibo.sdk.android.net.RequestListener;

/**
 * 提供新浪微博微博的方法访问，以实现信息的获取
 * 
 * @author norsie
 * @create 13-01-12
 */

public class WeiboAPIGather
{
    private StatusesAPI statusesAPI;
    private AccountAPI accountAPI;
    private UsersAPI userAPI;
    private Handler mHandler;
    private FriendshipsAPI friendshipsAPI;
    private Message message = new Message();
    private HashMap<String, String> accountInfo;

    public WeiboAPIGather(HashMap<String, String> mAccountInfo, Handler handler)
    {
        BaseApplication.sinaAccessToken = new Oauth2AccessToken(
                mAccountInfo.get(Constants.ACCESS_TOKEN),
                mAccountInfo.get(Constants.EXPIRES_IN));
        this.statusesAPI = new StatusesAPI(BaseApplication.sinaAccessToken);
        this.accountAPI = new AccountAPI(BaseApplication.sinaAccessToken);
        this.userAPI = new UsersAPI(BaseApplication.sinaAccessToken);
        this.friendshipsAPI=new FriendshipsAPI(BaseApplication.sinaAccessToken);
        this.mHandler = handler;
        this.accountInfo=mAccountInfo;
    }

     /*
      * 通过UID获取用户的详细信息
      */
     public void getUserShow(String uid)
     {
         userAPI.show(Long.parseLong(uid), new RequestListener() {

             @Override
             public void onIOException(IOException arg0)
             {
                 // TODO Auto-generated method stub

                 LogTools.error(arg0.toString());
             }

             @Override
             public void onError(WeiboException arg0)
             {
                 // TODO Auto-generated method stub
                 LogTools.error(arg0.toString());
             }

             @Override
             public void onComplete(String arg0)
             {
                 // TODO Auto-generated method stub
//                 mData = JsonTools.userShow(arg0);

                 message.what = Constants.MSG_WEIBO_USERSHOW_OK;
                 message.obj=JsonTools.userShow(arg0);
                 mHandler.sendMessage(message);
             }
         });
     }

     /**
      * 获取时间线微博信息-更新
      * 
      * @param Pages
      */
     public void getTimeline(int Pages)
     {
         if (Pages < 1)
             Pages = 1;
         statusesAPI.friendsTimeline(0L, 0L, 20, Pages, false, FEATURE.ALL,
                 false, new RequestListener() {

             @Override
             public void onComplete(String arg0)
             {
                 // TODO Auto-generated method stub
                 SNSContentManager.save(accountInfo.get(Constants.SNS_UID), arg0);
//                 mData = JsonTools.friendsTimeline(arg0);

                 message.what = Constants.MSG_WEIBO_TIMELINE_OK;
                 message.obj=JsonTools.friendsTimeline(arg0);
                 mHandler.sendMessage(message);
             }

             @Override
             public void onError(WeiboException arg0)
             {
                 // TODO Auto-generated method stub
                 message.what = Constants.MSG_WEIBO_TIMELINE_ERROR;
                 mHandler.sendMessage(message);
                 LogTools.info("friendsTimeline Error:"
                         + arg0.toString());
             }

             @Override
             public void onIOException(IOException arg0)
             {
                 // TODO Auto-generated method stub
                 LogTools.info("friendsTimeline IOException:"
                         + arg0.toString());
             }

         });

     }

     /**
      * 获取时间线微博信息-更新-提供json
      * 
      * @param Pages
      */
     public void getTimeline(String json)
     {
         message.what = Constants.MSG_WEIBO_TIMELINE_OK;
         message.obj=JsonTools.friendsTimeline(json);
         mHandler.sendMessage(message);

     }

     /**
      * 获取时间线微博信息-更多
      * 
      * @param max_id
      */
     public void getTimelineMore(Long max_id)
     {
         statusesAPI.friendsTimeline(0L, max_id + 1, 20, 1, false, FEATURE.ALL,
                 false, new RequestListener() {

             @Override
             public void onComplete(String arg0)
             {
                 message.what = Constants.MSG_WEIBO_TIMELINE_OK;
                 message.obj=JsonTools.friendsTimeline(arg0);
                 mHandler.sendMessage(message);
             }

             @Override
             public void onError(WeiboException arg0)
             {
                 message.what = Constants.MSG_WEIBO_TIMELINE_ERROR;
                 mHandler.sendMessage(message);
                 LogTools.info("friendsTimeline Error:"
                         + arg0.toString());
             }

             @Override
             public void onIOException(IOException arg0)
             {
                 // TODO Auto-generated method stub
                 LogTools.info("friendsTimeline IOException:"
                         + arg0.toString());
             }

         });
     }

     /**
      * 发布一条新微博（不含图）
      * 
      * @param content
      * @param lat
      * @param lon
      */
     public void setUpdate(String content, String lat, String lon)
     {
         statusesAPI.update(content, lat, lon, new RequestListener() {

             @Override
             public void onIOException(IOException arg0)
             {
                 // TODO Auto-generated method stub

             }

             @Override
             public void onError(WeiboException arg0)
             {
                 // TODO Auto-generated method stub
                 message.what = Constants.MSG_WEIBO_UPDATE_ERROR;
                 mHandler.sendMessage(message);
                 LogTools.info("Update Error:" + arg0.toString());
             }

             @Override
             public void onComplete(String arg0)
             {
                 // TODO Auto-generated method stub
                 message.what = Constants.MSG_WEIBO_UPDATE_OK;
                 mHandler.sendMessage(message);
             }
         });
     }

     /**
      * 发布一条新微博（含图）
      * 
      * @param content
      * @param lat
      * @param lon
      */
     public void setUpload(String status, String imageUrl, String lat, String lon)
     {
         statusesAPI.upload(status, imageUrl, lat, lon, new RequestListener() {

             @Override
             public void onIOException(IOException arg0)
             {
                 // TODO Auto-generated method stub

             }

             @Override
             public void onError(WeiboException arg0)
             {
                 // TODO Auto-generated method stub
                 message.what = Constants.MSG_WEIBO_UPLOAD_ERROR;
                 mHandler.sendMessage(message);
                 LogTools.info("Upload Error:" + arg0.toString());
             }

             @Override
             public void onComplete(String arg0)
             {
                 // TODO Auto-generated method stub
                 message.what = Constants.MSG_WEIBO_UPLOAD_OK;
                 mHandler.sendMessage(message);
             }
         });
     }

     /**
      * 关注
      * @param uid
      */
     public void setFollow(String uid) {
         friendshipsAPI.create(Long.parseLong(uid), "", new RequestListener() {

             @Override
             public void onIOException(IOException arg0)
             {
                 // TODO Auto-generated method stub

             }

             @Override
             public void onError(WeiboException arg0)
             {
                 // TODO Auto-generated method stub
                 LogTools.debug(arg0.toString());
             }

             @Override
             public void onComplete(String arg0)
             {
                 // TODO Auto-generated method stub
                 LogTools.debug("关注成功");
             }
         });
     }

}
